/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include"lcd.h"
#include "stdio.h"
#include "string.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
uint8_t B1_count,B2_count,B3_count,B4_count;
//uint8_t car_type,car_count,car_time;
//	char *car_type,*car_count,*car_time;
char buf[21],tx_buf[20],rx_buf[30],tx_buf1[20],tx_buf2[20];
uint8_t ucled = 0x00,rx_data,rx_count;
uint16_t uskey=0,lcdpage=0,a1=10,a2=25,a3=30,page=0;
uint8_t  key_val,key_down,key_old,key_up;
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
void Lcd_Display(void);
uint8_t Key_Scan(void){
	uint8_t key_val=0;
	uskey++;
	if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==GPIO_PIN_RESET)key_val=1;
	if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)==GPIO_PIN_RESET)key_val=2;
	if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)==GPIO_PIN_RESET)key_val=3;
	if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==GPIO_PIN_RESET)key_val=4;
	return key_val;
}

void Led_Disp(uint8_t ucled){
	HAL_GPIO_WritePin(GPIOC,0xFF<<8,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
	HAL_GPIO_WritePin(GPIOC,ucled<<8,GPIO_PIN_RESET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}

void Key_Proc(void){
	
	key_val=Key_Scan();
	key_down=key_val & (key_val ^key_old);
	key_up = ~key_val & (key_val ^key_old);
	key_old= key_val;
	//key_old 保存上一次检测按键值
	
//	if(key_down){
//	uskey=0;
//	}
//	if(uskey<100){
	
	//if(key_up==1){
///	ucled=0x04;
//	}
	
//	}
//	else {
//	if(key_val==1){
//	ucled=0x01;
//	}


	if(key_down==1 ){
Lcd_Display();
		//if(lcdpage++==3)lcdpage=0;
		
	
		
	}

		if(key_down==2){  
			
		if(++page==3)page=0;
Lcd_Display();
	}
		
	if(key_down==3){
		Lcd_Display();
	if(page==0)a1++;
	else if(page==1)a2++;
	else if(page==2)a3++;
		Lcd_Display();
	}
	
	if(key_down==4){
	if(page==0)a1--;
	else if(page==1)a2--;
	else if(page==2)a3--;
		Lcd_Display();
	}
}
	
	
	// if(key_up ==1){
//	ucled=0x00;
	//	Led_Disp(ucled);
	//}
	
void Lcd_Display(void){
	if(lcdpage==0){
			LCD_Clear(White);
		//LCD_SetBackColor(Red);
	//	LCD_SetTextColor(Green);
	
		
		if(page==0){
		//	LCD_Clear(White);
				LCD_SetTextColor(Black);
			LCD_DisplayStringLine(Line1,(uint8_t*)"   Parameter Setup");
			
			
			sprintf(buf,"   Threshold1: %dcm",a1);
			LCD_SetTextColor(Green);
		LCD_DisplayStringLine(Line3,(uint8_t*)buf);	
		sprintf(buf,"   Threshold1: %dcm",a2);
		LCD_SetTextColor(Black);
		LCD_DisplayStringLine(Line4,(uint8_t*)buf);	
		sprintf(buf,"   Threshold1: %dcm",a3);
		LCD_DisplayStringLine(Line5,(uint8_t*)buf);	}
		else if(page==1){
			//LCD_Clear(White);
			LCD_DisplayStringLine(Line1,(uint8_t*)"   Parameter Setup");
			
			sprintf(buf,"   Threshold1: %dcm",a1);
		//
		LCD_DisplayStringLine(Line3,(uint8_t*)buf);	
		sprintf(buf,"   Threshold1: %dcm",a2);
		LCD_SetTextColor(Green);
		LCD_DisplayStringLine(Line4,(uint8_t*)buf);	
		sprintf(buf,"   Threshold1: %dcm",a3);
		LCD_SetTextColor(Black);	
		LCD_DisplayStringLine(Line5,(uint8_t*)buf);	
		}
		else{
		//	LCD_Clear(White);
		//	LCD_DisplayStringLine(Line1,(uint8_t*)"   Parameter Setup");
			LCD_SetTextColor(Black);
			LCD_DisplayStringLine(Line1,(uint8_t*)"   Parameter Setup");
		sprintf(buf,"   Threshold1: %dcm",a1);
		LCD_DisplayStringLine(Line3,(uint8_t*)buf);	
		sprintf(buf,"   Threshold1: %dcm",a2);
		
		LCD_DisplayStringLine(Line4,(uint8_t*)buf);	
		sprintf(buf,"   Threshold1: %dcm",a3);
		//LCD_SetTextColor(Black);	
		LCD_SetTextColor(Green);	
		LCD_DisplayStringLine(Line5,(uint8_t*)buf);	
		
		}
	}
	if(lcdpage==1){
		LCD_Clear(Green);
		LCD_SetBackColor(Green);
	LCD_DisplayStringLine(Line1,(uint8_t*)"     this is page2");
		
		
	}
	if(lcdpage==2){
		LCD_Clear(Blue);
		LCD_SetBackColor(Blue);
	LCD_DisplayStringLine(Line1,(uint8_t*)"    this is page3");
		LCD_DisplayStringLine(Line2,(uint8_t*)"open the door!");
	}
	
}
	
void Uart_proc(void){
	if(rx_count==22){
//		if(strcmp(rx_buf,"LED0")==0){
//		ucled=0x01;
//			sprintf(tx_buf,"LED0 open\r\n");
//			HAL_UART_Transmit(&huart1,(uint8_t*)tx_buf,strlen(tx_buf),10);
//		}
//		if(strcmp(rx_buf,"LEDC")==0){
//		ucled=0x00;
//			sprintf(tx_buf,"LED0 closed\r\n");
//			HAL_UART_Transmit(&huart1,(uint8_t*)tx_buf,strlen(tx_buf),10);
//		}
	 char car_type1[20],count1[20],car_time[20];
		sscanf(rx_buf,"%4s:%4s:%12s",car_type1,count1,car_time);
		
		
		sprintf(tx_buf,"TYPE:%4s count:%4s\r\n",car_type1,count1);
	//	sprintf(tx_buf,"type:%4s  count:%4s  time:%4s\r\n",car_type1,count1,car_time);
		HAL_UART_Transmit(&huart1,(uint8_t*)tx_buf,strlen(tx_buf),50);
//		sprintf(tx_buf1,"count:%4s\r\n",count1);
//		HAL_UART_Transmit(&huart1,(uint8_t*)tx_buf1,strlen(tx_buf1),50);
//	sprintf(tx_buf,"time:%4s\r\n",car_time);
//	HAL_UART_Transmit(&huart1,(uint8_t*)tx_buf,strlen(tx_buf),50);
		rx_count=0;
		memset(rx_buf,0,20);
	}
}

void Lcd_PRoc(void){
	uint8_t ha=Key_Scan();
	if(ha==1)B1_count++;
	else if(ha ==2)B2_count++;
	else if(ha==3)B3_count++;
	else if(ha==4)B4_count++;
	
	sprintf(buf,"B1_count:%d",B1_count);
	LCD_DisplayStringLine(Line1,(uint8_t*)buf);
	sprintf(buf,"B2_count:%d",B2_count);
	LCD_DisplayStringLine(Line2,(uint8_t*)buf);
	sprintf(buf,"B3_count:%d",B3_count);
	LCD_DisplayStringLine(Line3,(uint8_t*)buf);
	sprintf(buf,"b4_count:%d",B4_count);
	LCD_DisplayStringLine(Line4,(uint8_t*)buf);
	
}
void uart_1(void){


}


/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */






/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */
  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
LCD_Init();

HAL_UART_Receive_IT(&huart1,&rx_data,1);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	//  if(lcdpage==0)LCD_Clear(Black);
	//  Lcd_Display();
	  
	  sprintf(buf,"hello\r\n");
	 // HAL_UART_Transmit(&huart1,(uint8_t*)buf,strlen(buf),10);
	  if (uskey>2000)uskey=0;
	//if(lcdpage==0)Lcd_Display();
	  //sprintf(buf,"yanshi:%d",uskey);
	 // LCD_DisplayStringLine(Line9,(uint8_t*)buf);
	  char *ptr="hello world";
	//  LCD_SetBackColor(Red);
	//  LCD_DisplayStringLine(Line9,ptr);
	  Led_Disp(ucled);
	  // Lcd_PRoc();
	 Key_Proc();
	  Uart_proc();
	  
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;
  RCC_OscInitStruct.PLL.PLLN = 20;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
//	if(rx_data=='0'){
//	ucled=0x01;
//	sprintf(tx_buf,"LED1 open\r\n");
//	HAL_UART_Transmit(&huart1,(uint8_t*)tx_buf,strlen(tx_buf),10);
//	//ucled=0x01;
//	Led_Disp(ucled);
//	}
//	else if(rx_data=='C'){
//	ucled=0x00;
//	sprintf(tx_buf,"LED1 closed\r\n");
//	HAL_UART_Transmit(&huart1,(uint8_t*)tx_buf,strlen(tx_buf),10);
//		Led_Disp(ucled);
//	
//	}
//	else {
//	sprintf(tx_buf,"error \r\n");
//	HAL_UART_Transmit(&huart1,(uint8_t*)tx_buf,strlen(tx_buf),10);
//		
//	}
	
//	uint8_t a1=strlen(rx_data);
//	sprintf(tx_buf,"count:%s\r\n",a1);
//	HAL_UART_Transmit(&huart1,(uint8_t*)tx_buf,strlen(tx_buf),10);
	rx_buf[rx_count++]=rx_data;
	HAL_UART_Receive_IT(&huart1,&rx_data,1);

}


/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
